package com.punch.user.util;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.punch.common.model.SysMenu;
import com.punch.common.vo.user.menu.MenuTreeVO;
import com.punch.common.vo.user.menu.SimpleMenuTreeVO;
import com.punch.common.vo.user.menu.UserMenuTreeVO;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

public class MenuUtil {

    /**
     * 迭代处理菜单
     *
     * @param menuMap 数据库数据
     * @param pid     父id
     * @author wujj
     * @since 2020/6/9 18:25
     */
    public static List<MenuTreeVO> handlerMenuTree(Map<Long, List<SysMenu>> menuMap, Long pid) {
        List<MenuTreeVO> retList = new ArrayList<>();
        List<SysMenu> menuList = menuMap.get(pid);
        if (CollUtil.isEmpty(menuList)) {
            return retList;
        }
        menuList.forEach(sysMenu -> {
            MenuTreeVO menuTreeVO = new MenuTreeVO();
            BeanUtils.copyProperties(sysMenu, menuTreeVO);
            menuTreeVO.setMenuId(sysMenu.getId());
            retList.add(menuTreeVO);
            //查找子节点
            if (menuMap.get(sysMenu.getId()) != null) {
                menuTreeVO.setChildList(handlerMenuTree(menuMap, sysMenu.getId()));
            }
        });
        return retList;
    }

    /**
     * 迭代处理简单菜单
     *
     * @param menuMap 数据库数据
     * @param pid     父id
     * @author wujj
     * @since 2020/6/9 18:25
     */
    public static List<SimpleMenuTreeVO> handlerSimpleMenuTree(Map<Long, List<SysMenu>> menuMap, Long pid) {
        List<SimpleMenuTreeVO> retList = new ArrayList<>();
        List<SysMenu> menuList = menuMap.get(pid);
        if (CollUtil.isEmpty(menuList)) {
            return retList;
        }
        menuList.forEach(sysMenu -> {
            SimpleMenuTreeVO menuTreeVO = new SimpleMenuTreeVO();
            menuTreeVO.setMenuId(sysMenu.getId());
            menuTreeVO.setTitle(sysMenu.getTitle());
            retList.add(menuTreeVO);
            //查找子节点
            if (menuMap.get(sysMenu.getId()) != null) {
                menuTreeVO.setChildList(handlerSimpleMenuTree(menuMap, sysMenu.getId()));
            }
        });
        return retList;
    }

    /**
     * 处理用户菜单数据
     *
     * @param menuMap 数据库数据
     * @param pid     父id
     * @return List<UserMenuTreeVO>
     * @author wujj
     */
    public static List<UserMenuTreeVO> handlerUserMenuTree(Map<Long, List<SysMenu>> menuMap, Long pid) {
        List<UserMenuTreeVO> retList = new ArrayList<>();
        List<SysMenu> menuList = menuMap.get(pid);
        if (CollUtil.isEmpty(menuList)) {
            return retList;
        }
        menuList.forEach(sysMenu -> {
            UserMenuTreeVO userMenuTreeVO = new UserMenuTreeVO();
            BeanUtils.copyProperties(sysMenu, userMenuTreeVO);
            JSONObject meta = new JSONObject();
            meta.put("icon", sysMenu.getIcon());
            meta.put("title", sysMenu.getTitle());
            meta.put("keepAlive", sysMenu.getKeepAlive());
            userMenuTreeVO.setMeta(meta);
            userMenuTreeVO.setMenuId(sysMenu.getId());
            userMenuTreeVO.setSort(sysMenu.getSortBy());
            userMenuTreeVO.setHidden(sysMenu.getHidden() == 0);
            retList.add(userMenuTreeVO);
            //查找子节点
            if (menuMap.get(sysMenu.getId()) != null) {
                //如果是按钮
                if (menuMap.get(sysMenu.getId()).get(0).getType() == 2) {
                    List<String> btnList = new ArrayList<>();
                    menuMap.get(sysMenu.getId()).forEach(sysMenu1 -> {
                        btnList.add(sysMenu1.getFrontPerms());
                    });
                    meta.put("btnList", btnList);
                } else {
                    userMenuTreeVO.setChildren(handlerUserMenuTree(menuMap, sysMenu.getId()));
                }
            }
        });
        return retList.stream().sorted(Comparator.comparing(UserMenuTreeVO::getSort)).collect(Collectors.toList());
    }

    public static void handUserMenu(List<SysMenu> allMenuList, List<SysMenu> roleMenuList, Collection<Long> retSet) {
        roleMenuList.forEach(sysMenu -> {
            SysMenu selectMenu = allMenuList.stream().filter(userMenu -> userMenu.getId().equals(sysMenu.getId())).findAny().orElse(null);
            retSet.add(selectMenu.getId());
            //查找父级
            if (sysMenu.getParentId() != 0) {
                getParentMenu(allMenuList, sysMenu.getParentId(), retSet);
            }
            //查找子级
            if (sysMenu.getChildNumber() > 0) {
                getChildMenu(allMenuList, sysMenu.getId(), retSet);
            }
        });
    }

    private static void getParentMenu(List<SysMenu> allMenuList, Long parentId, Collection<Long> retSet) {
        if (parentId == 0) {
            return;
        }
        SysMenu parentMenu = allMenuList.stream().filter(userMenu -> userMenu.getId().equals(parentId)).findAny().orElse(null);
        if (parentMenu != null) {
            retSet.add(parentMenu.getId());
            getParentMenu(allMenuList, parentMenu.getParentId(), retSet);
        }
    }

    private static void getChildMenu(List<SysMenu> allMenuList, Long currentId, Collection<Long> retSet) {
        if (currentId == null) {
            return;
        }
        SysMenu childMenu = allMenuList.stream().filter(userMenu -> userMenu.getParentId().equals(currentId)).findAny().orElse(null);
        if (childMenu != null) {
            retSet.add(childMenu.getId());
            getChildMenu(allMenuList, childMenu.getId(), retSet);
        }
    }
}
